<?php

namespace app\api\controller;

use app\health\model\HealthCoupon;
use app\health\model\HealthCouponList;
use app\health\model\HealthUser;
use think\Log;

class Changyoupoint
{
    public function sendCoupon()
    {
        $param = request()->param();
        Log::record('畅由=>' . json_encode($param), 'debug');
        //参与签名的参数
        $signParam = request()->except(['sign']);
        ksort($signParam);
        $signString = '';
        // 输出排序后的参数名和值
        foreach ($signParam as $key => $value) {
            $signString .= $key . '=' . $value . '&';
        }
        $signString .= 'key=123456';
        $sign       = md5($signString);
        if ($sign !== $param['sign']) {
            return json([
                'resultCode' => "9999",
                'message'    => "签名错误",
            ]);
        }
        $param['mobile'] = '15511301855';
        //发放优惠券
        if (!empty($param['orderStatus']) && $param['orderStatus'] === '01' && !empty($param['mobile'])) {
            $user = HealthUser::where('mobile', $param['mobile'])->find();
            if ($user) {
                try {
                    HealthCoupon::changYouExchange(5, $user['id'], $param['orderId']);
                } catch (\Exception $exception) {
                    Log::record('用户[' . $param['mobile'] . ']领取优惠券失败:' . $exception->getMessage(), 'debug');
                }
            } else {
                Log::record('用户[' . $param['mobile'] . ']领取优惠券失败:该手机未注册', 'debug');
            }
        }
        return json([
            'resultCode' => "0000",
            'message'    => "成功",
        ]);
    }
}